package com.witmore.skyline.web.controller;

import com.witmore.skyline.web.controller.vo.result.ResponseResult;
import com.witmore.skyline.web.service.AddressParseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址解析控制器
 *
 * @author 豆子高
 * @since 2025-10-16
 */
@Slf4j
@RestController
@RequestMapping("api/develop/address")
@Tag(name = "地址解析管理", description = "设备地址解析相关接口")
public class AddressParseController {

    @Autowired
    private AddressParseService addressParseService;

    @PostMapping("/parse/device/{deviceId}")
    @Operation(summary = "解析单个设备地址", description = "解析指定设备的地址信息并更新到数据库")
    public ResponseResult<Boolean> parseDeviceAddress(
            @Parameter(description = "设备ID", required = true)
            @PathVariable Integer deviceId) {

        log.info("开始解析设备地址，设备ID: {}", deviceId);
        boolean result = addressParseService.parseAndUpdateDeviceAddress(deviceId);
        return ResponseResult.success(result);
    }

    @PostMapping("/parse/devices/batch")
    @Operation(summary = "批量解析设备地址", description = "批量解析指定设备列表的地址信息")
    public ResponseResult<Integer> batchParseDeviceAddress(
            @Parameter(description = "设备ID列表", required = true)
            @RequestBody List<Integer> deviceIds) {

        log.info("开始批量解析设备地址，设备数量: {}", deviceIds.size());
        int successCount = addressParseService.batchParseAndUpdateDeviceAddress(deviceIds);
        return ResponseResult.success(successCount);
    }

    @PostMapping("/parse/devices/empty")
    @Operation(summary = "解析设备表中所有空地址", description = "解析设备表中所有地址为空或省市区信息不完整的记录")
    public ResponseResult<Integer> parseAllEmptyDeviceAddresses() {

        log.info("开始解析设备表中所有空地址");
        int successCount = addressParseService.parseAllEmptyDeviceAddresses();
        return ResponseResult.success(successCount);
    }

    @PostMapping("/parse/project/{projectId}")
    @Operation(summary = "解析单个项目地址", description = "解析指定项目的地址信息并更新到数据库")
    public ResponseResult<Boolean> parseProjectAddress(
            @Parameter(description = "项目ID", required = true)
            @PathVariable Integer projectId) {

        log.info("开始解析项目地址，项目ID: {}", projectId);
        boolean result = addressParseService.parseAndUpdateProjectAddress(projectId);
        return ResponseResult.success(result);
    }

    @PostMapping("/parse/projects/batch")
    @Operation(summary = "批量解析项目地址", description = "批量解析指定项目列表的地址信息")
    public ResponseResult<Integer> batchParseProjectAddress(
            @Parameter(description = "项目ID列表", required = true)
            @RequestBody List<Integer> projectIds) {

        log.info("开始批量解析项目地址，项目数量: {}", projectIds.size());
        int successCount = addressParseService.batchParseAndUpdateProjectAddress(projectIds);
        return ResponseResult.success(successCount);
    }

    @PostMapping("/parse/projects/empty")
    @Operation(summary = "解析项目表中所有空地址", description = "解析项目表中所有地址为空或省市区信息不完整的记录")
    public ResponseResult<Integer> parseAllEmptyProjectAddresses() {

        log.info("开始解析项目表中所有空地址");
        int successCount = addressParseService.parseAllEmptyProjectAddresses();
        return ResponseResult.success(successCount);
    }

}
